package com.dmz.resold.apartment.service;

import com.dmz.resold.apartment.csp.CspQueryJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author lee
 */
@Slf4j
@Component
public class ReHBSendReq implements Job {
    public static String bankID = "104";
    public static String url = "";
    public static String lsh = "";
    private static CspQueryJob cspa;

    @Autowired
    private Handler handler;

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        // 获取上一个需要重发的流水号并且重发
        cspa = new CspQueryJob();
        try {
            String result = cspa.getHBResendLsh("20180611");
            if (!result.substring(4, 8).equals("9998")) {
                lsh = result.split("\\|")[1];// 上一次需重发处理的流水号
                String commandCode = result.split("\\|")[2].trim();// 上一次需重发流水的指令代码
                String revFlag = result.split("\\|")[3].trim();// 上一次需重发流水 对方接收的结果，true接收成功，false接收失败
                String content = result.split("\\|")[4].trim();// 上次需重发流水的反馈报文
                String status = "0";

                if (revFlag.equals("false")) {// 上个流水反馈失败，重新发送反馈报文
                    log.info("------------------------[DMZ->房管局]--------------------------------------");
                    log.info("银行ID:" + bankID);
                    log.info("上次接收失败，现重发的LSH:" + lsh);
                    log.info("上次接收失败，现重发的LSH指令为:" + commandCode);
                    log.info("划拨等待指令");
                    boolean resultFlag = false;// 上次流水处理结果初始化
                    // 20001\20002指令当出现"0030请稍后查询转账结果"时，不给房管局返回指令结果
                    if (result.substring(4, 8).equals("0000")) {
                        if (commandCode.equals("20001")) {
                            if (result.substring(4, 8).equals("0000")) {
                                resultFlag = true;
                                boolean revFlagnew = handler.sendCommand("104", lsh, "", "", resultFlag, "", content, "",
                                        status);
                            }
                        }
                    } else if ("9999".equals(result.substring(4, 8))) {
                        System.out.println("不发划拨等待指令");
                        log.info("不发划拨等待指令");
                    } else if ("0030".equals(result.substring(4, 8))) {
                        System.out.println(result.substring(4, 8));
                    } else if ("0039".equals(result.substring(4, 8))) {
                        resultFlag = false;
                        boolean revFlagnew = handler.sendCommand("104", lsh, "", "", resultFlag, "", content, "",
                                status);
                    } else {
                        System.out.println(result.substring(4, 8));
                    }
                }
            } else {
                log.info("不存在需要重发的数据!");
            }
        } catch (Exception e) {
            log.error("划拨发送异常:" , e);
        }
    }
}